<template>
  <div id="detail">
    <!-- 1.详情页导航栏 -->
    <detail-nav-bar class="nav-bar" @titleClick="titleClick" ref="nav" />
    <!-- 2.详情页可滚动区域 -->
    <scroll
      class="content"
      :pullUpLoad="true"
      ref="scroll"
      @scroll="contentScroll"
      :probeType="3"
    >
      <!-- 2.1轮播图 -->
      <detail-swiper :topImages="topImages" />
      <!-- 2.2详情页商品信息 -->
      <detail-base-info :goods="goods" />
      <detail-shop-info :shop="shop" />
      <detail-goods-info
        @detailImageLoad="detailImageLoad"
        :detailInfo="detailInfo"
      />
      <detail-param-info ref="params" :paramInfo="paramInfo" />
      <detail-comment-info ref="comment" :commentInfo="commentInfo" />
      <goods-list ref="recommend" :goods="recommend" />
    </scroll>
    <!-- 3.返回顶部按钮 -->
    <back-top v-show="isShowBackTop" @click.native="backTop" />
    <toast :isShow="show" :message="message" />
    <detail-bottom-bar @addCart="addToCart" />
  </div>
</template>

<script>
import DetailNavBar from "./childcomponents/DetailNavBar.vue";
import {
  getDetail,
  Goods,
  Shop,
  GoodsParam,
  getRecommend,
} from "network/detail";
import DetailSwiper from "./childcomponents/DetailSwiper.vue";
import DetailBaseInfo from "./childcomponents/DetailBaseInfo.vue";
import DetailShopInfo from "./childcomponents/DetailShopInfo.vue";
import Scroll from "../../components/common/scroll/Scroll.vue";
import DetailGoodsInfo from "./childcomponents/DetailGoodsInfo.vue";
import DetailParamInfo from "./childcomponents/DetailParamInfo.vue";
import DetailCommentInfo from "./childcomponents/DetailCommentInfo.vue";
import GoodsList from "../../components/content/goods/GoodsList.vue";
import { itemListenerMixin, backTopMixin } from "common/mixin";
import { debounce } from "common/utils";
import DetailBottomBar from "./childcomponents/DetailBottomBar.vue";
import { mapActions } from "vuex";
import Toast from "../../components/common/toast/Toast.vue";
export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar,
    Toast,
  },
  mixins: [itemListenerMixin, backTopMixin],
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommend: [],
      themeTopY: [],
      getThemeTopY: null,
      currentIndex: 0,
      count: 0,
      show: false,
      message: "",
    };
  },
  created() {
    this.iid = this.$route.params.iid;
    getDetail(this.iid).then((res) => {
      console.log(res);
      const data = res.data.result;
      this.topImages.push(...data.itemInfo.topImages);
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );
      this.shop = new Shop(data.shopInfo);
      this.detailInfo = data.detailInfo;
      this.paramInfo = new GoodsParam(
        data.itemParams.info,
        data.itemParams.rule
      );
      if (data.rate.cRate != 0) {
        this.commentInfo = data.rate.list[0];
      }
    });
    getRecommend().then((res) => {
      console.log(res);
      this.recommend = res.data.data.list;
    });
    this.getThemeTopY = debounce(() => {
      console.log("回调次数测试");
      this.themeTopY = [];
      this.themeTopY.push(0);
      this.themeTopY.push(this.$refs.params.$el.offsetTop - 44);
      this.themeTopY.push(this.$refs.comment.$el.offsetTop - 44);
      this.themeTopY.push(this.$refs.recommend.$el.offsetTop - 44);
      console.log(this.themeTopY);
    }, 100);
  },

  mounted() {
    console.log("mounted");
  },
  destroyed() {
    this.$bus.$off("itemImgLoad", this.itemImgListener);
  },
  methods: {
    ...mapActions(["addCart"]),
    titleClick(index) {
      console.log(index);
      this.$refs.scroll.scrollTo(0, -this.themeTopY[index], 200);
    },
    detailImageLoad() {
      this.getThemeTopY();
    },
    contentScroll(position) {
      const positionY = -position.y;
      let length = this.themeTopY.length;
      for (let i = 0; i < length; i++) {
        if (
          this.currentIndex !== i &&
          ((i < length - 1 &&
            positionY >= this.themeTopY[i] &&
            positionY < this.themeTopY[i * 1 + 1]) ||
            (i == length - 1 && positionY >= this.themeTopY[i]))
        ) {
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex;
        }
      }
      this.isShowBackTop = -position.y > 1000;
    },
    addToCart() {
      const product = {};
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.realPrice;
      product.iid = this.iid;
      product.count = this.count;
      this.addCart(product).then((res) => {
        this.$toast.show(res, 2000);
        console.log(res);
      });
    },
  },
};
</script>

<style scoped>
#detail {
  height: 100vh;
  touch-action: none;
}
.nav-bar {
  position: relative;
  z-index: 100;
  background-color: #fff;
}
.content {
  height: calc(100% - 44px - 49px);
  overflow: hidden;
}
</style>